
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:livan_log_tool/livan_log_tool.dart';

class LogDetail extends StatefulWidget {
  const LogDetail({Key? key}) : super(key: key);
  @override
  _LogDetailState createState() => _LogDetailState();
}

class _LogDetailState extends State<LogDetail> {

  FileSystemEntity? _fileEntity;
  String _detail = "";
  bool _isShow = false;
  @override
  void initState() {
    super.initState();
    /*//加速度 受重力影响
    accelerometerEvents.listen((AccelerometerEvent event) async {
      //[AccelerometerEvent (x: 0.0992431640625, y: 0.11407470703125, z: 9.776962280273438)]
      print(event);
      FileManager.instance.writeLog("x: ${event.x.abs().toInt()};y: ${event.y.abs().toInt()};z: ${event.z.abs().toInt()};");
      int value = 70;
      if (event.x.abs() > value ||
          event.y.abs() > value ||
          event.z.abs() > value) {
        if (!_isShow) {
          _isShow = true;
          dynamic result = await showDialog<bool>(
              builder: (BuildContext context) {
                return CupertinoAlertDialog(
                  title: Text("摇一摇"),
                  content: Icon(Icons.ios_share),
                );
              },
              context: context,
              barrierDismissible: true);
          _isShow = false;
        }
      }
    });*/

    //加速度 不受重力影响
    // userAccelerometerEvents.listen((UserAccelerometerEvent event) {});

    // //陀螺仪传感器
    // gyroscopeEvents.listen((GyroscopeEvent event) {
    //   //[GyroscopeEvent (x: 0.00042724609375, y: 0.0005340576171875, z: -0.0003204345703125)]
    //   // print(event);
    // });
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future<void> initData() async {
    var args = ModalRoute.of(context)?.settings.arguments;
    if (args == null) {
      print("argument is empty");
      return;
    }
    if (args is! FileSystemEntity) {
      print("argument is not file entity");
      return;
    }
    _fileEntity = args;
    String log = await LogManager.instance.readLogAsync(_fileEntity!.path);
    setState(() {
      _fileEntity = args;
      _detail = log;
    });
  }

  @override
  Widget build(BuildContext context) {
    initData().then((_) => {});
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(_fileEntity?.filename ?? "日志详情"),
      ),
      body: _bodyRender(),
    );
  }

  Widget _bodyRender() {
    if (_fileEntity == null) {
      return Container();
    }
    return ScrollConfiguration(
      behavior: MyBehavior(),
      child: ListView(
        children: [
          Text(_detail),
        ],
      ),
    );
  }
}


class MyBehavior  extends ScrollBehavior {
  @override
  Widget buildOverscrollIndicator(BuildContext context, Widget child, ScrollableDetails details) {
    if (Platform.isAndroid || Platform.isFuchsia) {
      return child;
    } else {
      return super.buildOverscrollIndicator(context, child, details);
    }
  }
}
